home *** CD-ROM | disk | FTP | other *** search
- #include <exec/types.h>
- #include <exec/memory.h>
- #include <intuition/intuition.h>
- #include <intuition/gadgetclass.h>
- #include <libraries/gadtools.h>
- #include <dos/dosextens.h>
- #include <graphics/gfx.h>
-
- #include <clib/exec_protos.h>
- #include <clib/intuition_protos.h>
- #include <clib/gadtools_protos.h>
- #include <clib/dos_protos.h>
- #include <clib/graphics_protos.h>
-
- #include <ctype.h>
- #include <stdlib.h>
- #include <string.h>
- #include "rp.h"
-
- #define AMIGA 0
- #define MAC 1
- #define IBM 2
-
- MODULE void updatedetabulate(void);
- MODULE ABOOL spaceeol(TEXT nextchar);
-
- MODULE struct Gadget *MX71_Source = NULL,
- *TE71_SourceTitle = NULL,
- *MX71_Dest = NULL,
- *TE71_DestTitle = NULL,
- *ST71_Pathname = NULL,
- *BU71_PathnameASL = NULL,
- *CB71_Detabulate = NULL,
- *IN71_TabSize = NULL,
- *BU71_Convert = NULL,
- *CB71_Unwrap = NULL;
- AGLOBAL struct Gadget* TE71_Status = NULL;
-
- // from rp.c
- IMPORT TEXT IOBuffer[LONGESTFIELD + 1];
- IMPORT ABOOL fillwindows, stop;
- IMPORT SBYTE page;
- IMPORT SWORD y;
- IMPORT TEXT asldir[VLONGFIELD + 1];
- IMPORT ULONG fillcolour;
- IMPORT struct NewGadget Gadget;
- IMPORT struct Gadget *BU99_Right,
- *BU99_Stop,
- *PrevGadPtr;
- IMPORT struct Window* MainWindowPtr;
- IMPORT struct Screen* ScreenPtr;
- IMPORT struct IconBase* IconBase;
- IMPORT struct SharedStruct shared;
-
- MODULE struct
- { ABOOL detabulate, unwrap;
- UWORD source, dest;
- ULONG tabsize;
- } eol =
- { FALSE, FALSE, IBM, AMIGA, 8
- };
-
- // EOL/tab converter
- MODULE STRPTR EOLOptions[4] =
- { (STRPTR) "LF (Amiga)",
- (STRPTR) "CR (Macintosh)",
- (STRPTR) "CR+LF (IBM-PC)",
- NULL
- };
-
- AGLOBAL void eol1(void)
- { shared.pathname[0] = 0;
-
- verynewwindow
- ( EOL1WIDTH, EOL1HEIGHT,
- "Report+: EOL/Tab Converter",
- BUTTONIDCMP | STRINGIDCMP | MXIDCMP
- );
- if (fillwindows)
- { SetAPen(MainWindowPtr->RPort, 0);
- RectFill(MainWindowPtr->RPort, 80, 146, EOL1WIDTH - 10, 146 + 12); // TE71_Status
- }
-
- /* source title */
- setgadget(62 + 2, 40, 100, 12, "_Source:", PLACETEXT_ABOVE);
- // be sure this never overlaps with MX71_Source
- TE71_SourceTitle = PrevGadPtr = (struct Gadget *) CreateGadget
- ( TEXT_KIND,
- PrevGadPtr,
- &Gadget,
- GT_Underscore, '_',
- TAG_DONE
- );
- /* source format */
- setgadget(62, 50, 0, 0, NULL, PLACETEXT_RIGHT);
- MX71_Source = PrevGadPtr = (struct Gadget *) CreateGadget
- ( MX_KIND,
- PrevGadPtr,
- &Gadget,
- GTMX_Labels, &EOLOptions,
- GTMX_Active, eol.source,
- TAG_DONE
- );
- /* dest title */
- setgadget(314 + 22, 40, 100, 12, "_Destination:", PLACETEXT_ABOVE);
- // be sure this never overlaps with MX71_Dest
- TE71_DestTitle = PrevGadPtr = (struct Gadget *) CreateGadget
- ( TEXT_KIND,
- PrevGadPtr,
- &Gadget,
- GT_Underscore, '_',
- TAG_DONE
- );
- /* destination format */
- setgadget(314, 50, 0, 0, NULL, PLACETEXT_RIGHT);
- MX71_Dest = PrevGadPtr = (struct Gadget *) CreateGadget
- ( MX_KIND,
- PrevGadPtr,
- &Gadget,
- GTMX_Labels, &EOLOptions,
- GTMX_Active, eol.dest,
- TAG_DONE
- );
-
- /* pathname */
- setgadget(132, 88, 340, 12, "_Pathname(s):", NULL);
- ST71_Pathname = PrevGadPtr = (struct Gadget *) CreateGadget
- ( STRING_KIND,
- PrevGadPtr,
- &Gadget,
- GTST_String, shared.pathname,
- GTST_MaxChars, LONGESTFIELD,
- GA_TabCycle, TRUE,
- GT_Underscore, '_',
- TAG_DONE
- );
- /* pathname... */
- setgadget(474, 88, 28, 12, "_...", NULL);
- BU71_PathnameASL = PrevGadPtr = (struct Gadget *) CreateGadget
- ( BUTTON_KIND,
- PrevGadPtr,
- &Gadget,
- GT_Underscore, '_',
- TAG_DONE
- );
-
- // 'convert tabs to spaces?'
- setgadget(294, 110, 0, 0, "C_onvert tabs to spaces?", NULL);
- CB71_Detabulate = PrevGadPtr = (struct Gadget *) CreateGadget
- ( CHECKBOX_KIND,
- PrevGadPtr,
- &Gadget,
- GTCB_Checked, eol.detabulate,
- GT_Underscore, '_',
- TAG_DONE
- );
- /* tab size */
- setgadget(408, 110, 32, 12, "_Tab size:", NULL);
- IN71_TabSize = PrevGadPtr = (struct Gadget *) CreateGadget
- ( INTEGER_KIND,
- PrevGadPtr,
- &Gadget,
- GTIN_Number, eol.tabsize,
- GTIN_MaxChars, 2,
- GA_TabCycle, TRUE,
- GT_Underscore, '_',
- TAG_DONE
- );
-
- setgadget(294, 122, 0, 0, "_Unwrap words?", NULL);
- CB71_Unwrap = PrevGadPtr = (struct Gadget *) CreateGadget
- ( CHECKBOX_KIND,
- PrevGadPtr,
- &Gadget,
- GTCB_Checked, eol.unwrap,
- GT_Underscore, '_',
- TAG_DONE
- );
-
- setgadget(80, 146, EOL1WIDTH - 80 - 10, 12, "Status:", NULL);
- TE71_Status = PrevGadPtr = (struct Gadget *) CreateGadget
- ( TEXT_KIND,
- PrevGadPtr,
- &Gadget,
- GTTX_Text, "Ready.",
- GTTX_Border, TRUE,
- TAG_DONE
- );
- setgadget(10, EOL1HEIGHT - 28, (EOL1WIDTH / 2) - 10, 12, "_Convert", NULL);
- BU71_Convert = PrevGadPtr = (struct Gadget *) CreateGadget
- ( BUTTON_KIND,
- PrevGadPtr,
- &Gadget,
- GT_Underscore, '_',
- TAG_DONE
- );
- setgadget(EOL1WIDTH / 2, EOL1HEIGHT - 28, (EOL1WIDTH / 2) - 10, 12, "Stop", NULL);
- BU99_Stop = PrevGadPtr = (struct Gadget *) CreateGadget
- ( BUTTON_KIND,
- PrevGadPtr,
- &Gadget,
- GT_Underscore, '_',
- GA_Disabled, TRUE,
- TAG_DONE
- );
-
- drawgadgets((UWORD) ~0);
- ActivateGadget(ST71_Pathname, MainWindowPtr, NULL);
- if (eol.detabulate)
- { GT_SetGadgetAttrs
- ( IN71_TabSize,
- MainWindowPtr, NULL,
- GA_Disabled, FALSE,
- TAG_DONE
- );
- } else
- { GT_SetGadgetAttrs
- ( IN71_TabSize,
- MainWindowPtr, NULL,
- GA_Disabled, TRUE,
- TAG_DONE
- );
- }
- loop();
- closewindow();
- }
-
- AGLOBAL void eol_do(void)
- { strcpy
- ( shared.pathname,
- ((struct StringInfo *) ST71_Pathname->SpecialInfo)->Buffer
- );
- eol.tabsize = ((struct StringInfo *) IN71_TabSize->SpecialInfo)->LongInt;
-
- GT_SetGadgetAttrs
- ( BU99_Stop,
- MainWindowPtr, NULL,
- GA_Disabled, FALSE,
- TAG_DONE
- );
- GT_SetGadgetAttrs
- ( MX71_Source,
- MainWindowPtr, NULL,
- GA_Disabled, TRUE,
- TAG_DONE
- );
- GT_SetGadgetAttrs
- ( MX71_Dest,
- MainWindowPtr, NULL,
- GA_Disabled, TRUE,
- TAG_DONE
- );
- GT_SetGadgetAttrs
- ( ST71_Pathname,
- MainWindowPtr, NULL,
- GA_Disabled, TRUE,
- TAG_DONE
- );
- GT_SetGadgetAttrs
- ( BU71_PathnameASL,
- MainWindowPtr, NULL,
- GA_Disabled, TRUE,
- TAG_DONE
- );
- GT_SetGadgetAttrs
- ( CB71_Detabulate,
- MainWindowPtr, NULL,
- GA_Disabled, TRUE,
- TAG_DONE
- );
- GT_SetGadgetAttrs
- ( IN71_TabSize,
- MainWindowPtr, NULL,
- GA_Disabled, TRUE,
- TAG_DONE
- );
- GT_SetGadgetAttrs
- ( BU71_Convert,
- MainWindowPtr, NULL,
- GA_Disabled, TRUE,
- TAG_DONE
- );
- GT_SetGadgetAttrs
- ( BU99_Right,
- MainWindowPtr, NULL,
- GA_Disabled, TRUE,
- TAG_DONE
- );
-
- convert(TRUE);
-
- if (!stop)
- { GT_SetGadgetAttrs
- ( TE71_Status,
- MainWindowPtr, NULL,
- GTTX_Text, "All done.",
- TAG_DONE
- );
- }
- GT_SetGadgetAttrs
- ( BU99_Stop,
- MainWindowPtr, NULL,
- GA_Disabled, TRUE,
- TAG_DONE
- );
- GT_SetGadgetAttrs
- ( MX71_Source,
- MainWindowPtr, NULL,
- GA_Disabled, FALSE,
- TAG_DONE
- );
- GT_SetGadgetAttrs
- ( MX71_Dest,
- MainWindowPtr, NULL,
- GA_Disabled, FALSE,
- TAG_DONE
- );
- GT_SetGadgetAttrs
- ( ST71_Pathname,
- MainWindowPtr, NULL,
- GA_Disabled, FALSE,
- TAG_DONE
- );
- GT_SetGadgetAttrs
- ( BU71_PathnameASL,
- MainWindowPtr, NULL,
- GA_Disabled, FALSE,
- TAG_DONE
- );
- GT_SetGadgetAttrs
- ( CB71_Detabulate,
- MainWindowPtr, NULL,
- GA_Disabled, FALSE,
- TAG_DONE
- );
- GT_SetGadgetAttrs
- ( IN71_TabSize,
- MainWindowPtr, NULL,
- GA_Disabled, FALSE,
- TAG_DONE
- );
- GT_SetGadgetAttrs
- ( BU71_Convert,
- MainWindowPtr, NULL,
- GA_Disabled, FALSE,
- TAG_DONE
- );
- GT_SetGadgetAttrs
- ( BU99_Right,
- MainWindowPtr, NULL,
- GA_Disabled, FALSE,
- TAG_DONE
- );
- }
-
- AGLOBAL void eolconvert(ABOOL gui)
- { ULONG gap, i, j, cursorx = 0;
- ABOOL ok = TRUE;
- struct FileInfoBlock InputInfoBlock;
- UBYTE* InputBufferPtr; // always use UBYTE not APTR for pointer arithmetic
- BPTR FileHandle;
- TEXT saystring[VLONGFIELD + 1];
-
- strcpy(saystring, "Converting ");
- strcat(saystring, shared.thisfile);
- strcat(saystring, "...");
- if (gui)
- { GT_SetGadgetAttrs
- ( TE71_Status,
- MainWindowPtr, NULL,
- GTTX_Text, saystring,
- TAG_DONE
- );
- } else
- { Printf("%s", saystring);
- Flush(Output());
- }
-
- if (FileHandle = (BPTR) Lock(shared.thisfile, ACCESS_READ))
- { if (Examine(FileHandle, &InputInfoBlock))
- { UnLock(FileHandle);
- // FileHandle = NULL;
- if (InputInfoBlock.fib_DirEntryType == -3) // if a file
- { // if it is a directory, we would ideally like to convert all
- // files in that directory. We also should support links.
- if (InputBufferPtr = AllocMem(InputInfoBlock.fib_Size, MEMF_ANY | MEMF_PUBLIC))
- { if (FileHandle = (BPTR) Open(shared.thisfile, MODE_OLDFILE))
- { if (Read(FileHandle, InputBufferPtr, InputInfoBlock.fib_Size) != -1)
- { Close(FileHandle);
- if (FileHandle = (BPTR) Open(shared.thisfile, MODE_NEWFILE))
- { for (i = 0; i < InputInfoBlock.fib_Size; i++)
- { if (*(InputBufferPtr + i) == LF)
- { if (eol.unwrap && (eol.source == AMIGA || eol.source == IBM) && spaceeol(*(InputBufferPtr + i + 1)))
- { cursorx++;
- FPutC(FileHandle, ' ');
- } else
- { cursorx = 0;
-
- if (eol.source == eol.dest)
- FPutC(FileHandle, LF); // pass it through
- elif (eol.source == AMIGA && eol.dest == IBM)
- { FPutC(FileHandle, CR); // add this
- FPutC(FileHandle, LF); // and pass this through
- } elif (eol.source == AMIGA && eol.dest == MAC)
- FPutC(FileHandle, CR); // convert from LF to CR
- elif (eol.source == IBM && eol.dest == AMIGA)
- FPutC(FileHandle, LF); // pass it through
- elif (eol.source == IBM && eol.dest == MAC)
- ; // do nothing
- else ; // mac as source: we never expect LFs
- } }
- elif (*(InputBufferPtr + i) == CR)
- { if (eol.unwrap && eol.source == MAC && spaceeol(*(InputBufferPtr + i + 1)))
- { cursorx++;
- FPutC(FileHandle, ' ');
- } else
- { cursorx = 0;
-
- if (eol.source == eol.dest)
- FPutC(FileHandle, CR); // pass it through
- elif (eol.source == IBM && eol.dest == AMIGA)
- ; // do nothing
- elif (eol.source == IBM && eol.dest == MAC)
- FPutC(FileHandle, CR); // pass it through
- elif (eol.source == MAC && eol.dest == AMIGA)
- FPutC(FileHandle, LF); // convert from CR to LF
- elif (eol.source == MAC && eol.dest == IBM)
- { FPutC(FileHandle, CR); // pass this through
- FPutC(FileHandle, LF); // and add this
- } else ; // amiga as source: we never expect CRs
- } }
- elif (*(InputBufferPtr + i) == TAB)
- { if (eol.detabulate)
- { gap = (((cursorx / eol.tabsize) + 1) * eol.tabsize) - cursorx;
- for (j = 1; j <= gap; j++)
- FPutC(FileHandle, ' ');
- cursorx += gap;
- } else FPutC(FileHandle, TAB); // no point to cursorx in non-detabulate
- } else
- { FPutC(FileHandle, *(InputBufferPtr + i));
- cursorx++;
- } }
- /* Note that FPutC() returns EOF for errors; this is not currently
- checked. */
- ok = TRUE;
- Close(FileHandle);
- } }
- else
- { Close(FileHandle);
- } }
- FreeMem(InputBufferPtr, InputInfoBlock.fib_Size);
- } } }
- else
- { UnLock(FileHandle);
- } }
-
- if (gui)
- { if (ok)
- strcat(saystring, "done.");
- else strcat(saystring, "failed!");
- GT_SetGadgetAttrs
- ( TE71_Status,
- MainWindowPtr, NULL,
- GTTX_Text, saystring,
- TAG_DONE
- );
- } else
- { if (ok)
- Printf("done.\n");
- else Printf("failed!\n");
- }
-
- checkabort(gui);
- }
-
- AGLOBAL void eol_loop(ULONG class, struct Gadget* addr, UWORD code, UWORD qual)
- { if (page == 71)
- { if (class == IDCMP_VANILLAKEY)
- { code = toupper(code);
- if (code == 'S')
- { if (!(qual & IEQUALIFIER_LSHIFT) && !(qual & IEQUALIFIER_RSHIFT))
- { if (eol.source < 2)
- eol.source++;
- else eol.source = 0;
- } else
- { if (eol.source > 0)
- eol.source--;
- else eol.source = 2;
- }
- GT_SetGadgetAttrs
- ( MX71_Source,
- MainWindowPtr,
- NULL,
- GTMX_Active, eol.source,
- TAG_DONE
- );
- } elif (code == 'D')
- { if (!(qual & IEQUALIFIER_LSHIFT) && !(qual & IEQUALIFIER_RSHIFT))
- { if (eol.dest < 2)
- eol.dest++;
- else eol.dest = 0;
- } else
- { if (eol.dest > 0)
- eol.dest--;
- else eol.dest = 2;
- }
- GT_SetGadgetAttrs
- ( MX71_Dest,
- MainWindowPtr,
- NULL,
- GTMX_Active, eol.dest,
- TAG_DONE
- );
- } elif (code == 'O')
- { if (CB71_Detabulate->Flags & GFLG_SELECTED)
- { eol.detabulate = FALSE;
- } else eol.detabulate = TRUE;
- updatedetabulate();
- } elif (code == 'U')
- { if (CB71_Unwrap->Flags & GFLG_SELECTED)
- { eol.unwrap = FALSE;
- } else eol.unwrap = TRUE;
- GT_SetGadgetAttrs
- ( CB71_Unwrap,
- MainWindowPtr, NULL,
- GTCB_Checked, eol.unwrap,
- TAG_DONE
- );
- } elif (code == 'T')
- { ActivateGadget(IN71_TabSize, MainWindowPtr, NULL);
- } elif (code == 'C')
- { eol_do();
- } elif (code == 'P')
- { ActivateGadget(ST71_Pathname, MainWindowPtr, NULL);
- } elif (code == ESCAPE)
- { page = 0;
- } elif (code == '.')
- { multiasl("~(#?.info)");
- GT_SetGadgetAttrs
- ( ST71_Pathname,
- MainWindowPtr,
- NULL,
- GTST_String, shared.pathname,
- TAG_DONE
- );
- } }
- elif (class == IDCMP_GADGETDOWN)
- { // radio gadgets
- if (addr == MX71_Source)
- eol.source = code;
- elif (addr == MX71_Dest)
- eol.dest = code;
- } elif (class == IDCMP_GADGETUP)
- { // button, etc. gadgets
- if (addr == BU99_Right)
- page = 0;
- elif (addr == BU71_Convert)
- { eol_do();
- } elif (addr == BU71_PathnameASL)
- { multiasl("~(#?.info)");
- GT_SetGadgetAttrs
- ( ST71_Pathname,
- MainWindowPtr,
- NULL,
- GTST_String, shared.pathname,
- TAG_DONE
- );
- } elif (addr == CB71_Detabulate)
- { if (CB71_Detabulate->Flags & GFLG_SELECTED)
- { eol.detabulate = TRUE;
- } else eol.detabulate = FALSE;
- updatedetabulate();
- } elif (addr == CB71_Unwrap)
- { if (CB71_Unwrap->Flags & GFLG_SELECTED)
- { eol.unwrap = TRUE;
- } else eol.unwrap = FALSE;
- GT_SetGadgetAttrs
- ( CB71_Unwrap,
- MainWindowPtr, NULL,
- GTCB_Checked, eol.unwrap,
- TAG_DONE
- );
- } } }
- else
- { // assert(page == 72);
- if (class == IDCMP_GADGETUP)
- { if (addr == BU99_Right)
- page = 0;
- } elif (class == IDCMP_VANILLAKEY)
- { if (code == ESCAPE)
- page = 0;
- } } }
-
- MODULE void updatedetabulate(void)
- { GT_SetGadgetAttrs
- ( CB71_Detabulate,
- MainWindowPtr, NULL,
- GTCB_Checked, eol.detabulate,
- TAG_DONE
- );
- if (eol.detabulate)
- { GT_SetGadgetAttrs
- ( IN71_TabSize,
- MainWindowPtr, NULL,
- GA_Disabled, FALSE,
- TAG_DONE
- );
- } else
- { GT_SetGadgetAttrs
- ( IN71_TabSize,
- MainWindowPtr, NULL,
- GA_Disabled, TRUE,
- TAG_DONE
- );
- } }
-
- MODULE ABOOL spaceeol(TEXT nextchar)
- { if
- ( nextchar == ' '
- || nextchar == TAB
- || nextchar == LF
- || nextchar == CR
- )
- { return(FALSE);
- } else return(TRUE);
- }
-
- AGLOBAL void eol_die(void)
- { IOBuffer[1] = (UBYTE) eol.source;
- IOBuffer[2] = (UBYTE) eol.dest;
- IOBuffer[3] = eol.detabulate;
- IOBuffer[4] = (UBYTE) eol.tabsize;
- IOBuffer[5] = eol.unwrap;
- }
-
- AGLOBAL void eol_config(void)
- { eol.source = IOBuffer[1];
- eol.dest = IOBuffer[2];
- eol.detabulate = IOBuffer[3];
- eol.tabsize = IOBuffer[4];
- eol.unwrap = IOBuffer[5];
- }
-